﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenExpressApp.MetaAttribute;
using Csla.Wpf;
using OpenExpressApp.Command;
using Csla.Core;
using System.ComponentModel;
using System.Windows;
using OpenExpressApp.Template;
using OpenExpressApp.Types;
using Csla;
using OpenExpressApp.WPF.Controls;
using System.Collections.ObjectModel;
using log4net;
using System.Collections;
using OpenExpressApp.MetaModel;
using Avalon.Windows.Controls;
using OpenExpressApp.WPF.Templete;
using OpenExpressApp.Module;
using OpenExpressApp.Module.WPF;
using System.Windows.Controls;
using Ricciolo.Controls;
using System.Reflection;
using System.Windows.Markup;
using System.Diagnostics;
using AvalonDock;
using OpenExpressApp.Module.WPF.Editors;
using Csla.OpenExpressApp;


namespace OpenExpressApp.WPF.Command
{
    [Command(CommandNames.Cancel, ImageName = "Cancel.bmp", Label = "取消", ToolbarType = ToolbarType.Main, ModuleType = ModuleType.Bill, ToolTip = "取消更改")]
    public class CancelCommand : WPFViewCommand
    {
        public override bool CanExecute(ObjectView view)
        {
            var currentObject = view.CurrentObject as ITrackStatus;
            return currentObject != null &&
                currentObject.IsDirty &&
                currentObject.IsNew == false;
        }

        //为了不使用BeginEdit导致EditLevel莫名错误，Cancel使用重新获取数据
        public override void Execute(ObjectView view)
        {

            var listDetailFrameController = (view.Control as FrameworkElement).GetFrameTemplate().Controller as IListDetailFrameController;
            listDetailFrameController.AsyncGetCurObject(true);
            
            //由于树形对象parent和children不好beginedit和canceledit，这里先重新获取一遍数据
            //var listDetailFrameController = (view.Control as FrameworkElement).GetFrameTemplate().Controller as IListDetailFrameController;
            //if (null != listDetailFrameController)
            //{
            //    listDetailFrameController.AsyncGetCurObject(true);
            //}
            //else
            //{
            //    var currentObject = view.CurrentObject as BusinessBase;
            //    currentObject.CancelEdit();
            //    currentObject.BeginEdit();

            //    //Refresh child's data
            //    var listObjectView = view as ListObjectView;
            //    if (listObjectView != null)
            //    {
            //        var detailView = listObjectView.DetailView;
            //        if (detailView != null)
            //        {
            //            detailView.CurrentObject = view.CurrentObject;
            //        }
            //    }
            //}
        }
    }
}
